home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
8bit
/
cislib_b
/
strarr.doc
< prev
next >
Wrap
Text File
|
1995-04-22
|
4KB
|
105 lines
.12
4 1 5 0 10 70 2 12 132
3STRING ARRAYS FOR THE ATARI
.
.
.
.1by
.
.
.David H. Neal
.
.
.70707,724
Arvada, CO
.
.
.
.4A number of programs have been written to simulate string arrays on the
Atari computers. Here is another one, perhaps a bit more elegant than some,
in that it avoids filling unused portions of the array with blanks. This
one has no unused areas. Basically, the technique involves finding the
delimiters for string segmenting; i.e., in A$(x,y), we will find x and
y for each variable in the array and use x and y to select the variable
desired. I will offer two examples to demonstrate the technique.
.
.The first example is representative of the case where we have a known,
fixed number of elements of equal length, as for example, in a list of
the months of the year, abbrviated to three letters. The complete string
is
MONTH$="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
The individual element might be called MON$,and we might want to list the
name of the numerical month, M. We might call the delimiters M1 and M2,
so that MON$ is given by
MON$ = MONTH$(M1,M2)
For elements of 3 letters, M1 is
M1 = M + 2 * (m-1)
and M2 = M1 + 2
A program to list the numerical month with its 3-letter abbrviation would
be:
10 DIM MONTH$(36),MON$(3)
20 MONTH$="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
30 ?"ENTER THE NUMERICAL MONTH";:INPUT M
40 M1=M+2*(M-1):M2=M1+2
50 MON$=MONTH$(M1,M2)
60 ? M,MON$
70 GOTO 30
.
.M1 and M2 can be similarly derived for any situation where a full string
can be composed of elements of equal length. A general equation for M1
and M2 is:
M1 = M + A*(M-1)
M2 = M1 + A
where A is 1 less than the length of the elements.
.
.The second case is one in which we have an unknown number of elements and
they are of variable length. This is the case that has traditionally been
handled by filling a string with spaces. An example might be the entering
of names and their random recall. We can apply the same principle that
we used for the months example, except that now we are forced to deal with
an unknown element length.
.
.Let us suppose that our program requirements are 10 names, and we have
decided to allow each name to be up to 10 characters (including spaces)
long. So,
DIM ALLNAM$(100),NAME$(10)
We also need, for this case, two arrays, L1 and L2 of 10 elements each.
So,
DIM L1(10),L2(10)
At the beginning, the total length of our string is zero, so
TOL=0
For each name that we input in succession, the beginning element is the
total length of our string so far plus 1, or
L1(I) = TOL + 1
and the ending element is L1 plus the length of the current name minus
1, or
L2(I) = L1(I) + LEN(NAME$) - 1
Now the total length of our string is
TOL = TOL + LEN(NAME$)
and the portion of ALLNAM$ that holds the name we just entered is
ALLNAM$(L1(I),L2(I)) = NAME$
We can now write a complete program segment for an array of 10 names, each
a maximum length of 10 characters.
10 DIM ALLNAM$(100),NAME$(10),L1(10),L2(10)
20 TOL=0
30 ?"ENTER NUMBER OF NAMES UP TO 10";:INPUT N
40 IF N>10 THEN 30
50 FOR I=1 TO N
60 ?"ENTER NAME";:INPUT NAME$
70 L1(I)=TOL+1:L2(I)=L1(I)+LEN(NAME$)-1:TOL=TOL+LEN(NAME$)
80 ALLNAM$(L1(I),L2(I))=NAME$:NEXT I
Now we need a method to get back the names we entered. if we know the sequenti
al number all we need is
NAME$=ALLNAM$(L1(I),L2(I))
But, we may want to match a particular name with some data in another array.
We have to do a search as in the following program segment:
90 ?"ENTER NAME";:INPUT NAME$
100 FOR I=1 TO N:IF NAME$=ALLNAM$(L1(I),L2(I)) THEN 120
110 NEXT I:?"NAME NOT FOUND--TRY AGAIN":GOTO 90
120 ?"NAME FOUND"
.
.These methods comprise a direct, easily used technique for simulating string
arrays on the Atari home computers.
-30-